package com.miao.elm.controller;

import com.miao.elm.entity.OrdersEntity;
import com.miao.elm.service.OrdersService;
import com.miao.elm.to.OrderTo;
import com.miao.elm.to.OrdersTo;
import com.miao.elm.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("OrdersController")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    //创建订单
    @PostMapping("/createOrders")
    public String createOrders(OrderTo orderTo) throws ExecutionException, InterruptedException {
        return ordersService.createOrders(orderTo);
    }

    //根据订单编号查询订单信息
    @PostMapping("/getOrdersById")
    public OrderTo getOrdersById(String orderId){
        return ordersService.getOrdersById(orderId);
    }

    //根据用户编号查询此用户的所有订单信息
    @PostMapping("/listOrdersByUserId")
    public List<OrdersTo> listOrdersByUserId(String userId){
        return ordersService.listOrdersByUserId(userId);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{orderId}")
    public R info(String orderId){
		OrdersEntity orders = ordersService.getById(orderId);
        return R.ok().put("orders", orders);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrdersEntity orders){
		ordersService.save(orders);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrdersEntity orders){
		ordersService.updateById(orders);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] orderIds){
		ordersService.removeByIds(Arrays.asList(orderIds));

        return R.ok();
    }

}
